﻿using System.Linq;
using SD.Commons.DataFilters;
using SD.DataAccess;
using System;

namespace SD.Business
{
    /// <summary>
    /// GovOfficeRegionBO class
    /// </summary>
    public static class GovOfficeRegionBus
    {
        /// <summary>
        /// GovOfficeRegionDao object
        /// </summary>
        private static readonly GovOfficeRegionDao Dao = new GovOfficeRegionDao();

        /// <summary>
        /// Gets the gov office regions.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="sortType">Type of the sort.</param>
        /// <param name="startRowIndex">Start index of the row.</param>
        /// <param name="maxRows">The max rows.</param>
        /// <returns>GovOfficeRegion list</returns>
        public static IQueryable<GovOfficeRegion> GetList(GovOfficeRegionFilter filter, string sortType,
                                                                      int startRowIndex, int maxRows)
        {
            return Dao.GetList(filter, sortType, startRowIndex, maxRows);
        }

        /// <summary>
        /// Gets the gov office region by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>GovOfficeRegion object</returns>
        public static GovOfficeRegion Get(int id)
        {
            return Dao.Get(id);
        }

        /// <summary>
        /// Gets the gov office regions count.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        public static int GetGovOfficeRegionsCount(GovOfficeRegionFilter filter)
        {
            return Dao.GetListCount(filter);
        }

        /// <summary>
        /// Check the name is existed in database 
        /// </summary>
        /// <param name="name">name to be checked</param>
        /// <returns>existed: true, else: false</returns>
        public static bool IsExistedName(string name)
        {
            return Dao.IsExistedName(name);
        }
        /// <summary>
        /// Check the name is existed (with different id) in database 
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="name">name</param>
        /// <returns>existed: true, else: false</returns>
        public static bool IsExistedName(int id, string name)
        {
            return Dao.IsExistedName(id, name);
        }
        /// <summary>
        /// Add new government office region object to database
        /// </summary>
        /// <param name="gor">government office region object</param>
        /// <returns>success: true, else: false </returns>
        public static bool Add(GovOfficeRegion gor)
        {
            if (!string.IsNullOrEmpty(gor.GovOfficeRegionName))
            {
                gor.GovOfficeRegionName = gor.GovOfficeRegionName.Trim();
            }
            else
            {
                throw new Exception("Government Office Region Name can not be null or empty.");
            }
            if (IsExistedName(gor.GovOfficeRegionName))
            {
                throw new Exception("This name is already existed in the system.");
            }
            return Dao.Add(gor);
        }
        /// <summary>
        /// Edit gorvernment office region object from databse and then 
        /// updating to database
        /// </summary>
        /// <param name="gor">government office region object</param>
        /// <returns>success: true, else: false</returns>
        public static bool Update(GovOfficeRegion gor)
        {
            if (!string.IsNullOrEmpty(gor.GovOfficeRegionName))
            {
                gor.GovOfficeRegionName = gor.GovOfficeRegionName.Trim();
            }
            else
            {
                throw new Exception("Government Office Region Name can not be null or empty.");
            }
            if (IsExistedName(gor.GovOfficeRegionId, gor.GovOfficeRegionName))
            {
                throw new Exception("This name is already existed in the system.");
            }
            return Dao.Update(gor);
        }
        /// <summary>
        /// Set active status for government office region object
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="status">Status</param>
        /// <returns>true or false</returns>
        public static bool SetActiveStatus(int id, bool status)
        {
            return Dao.SetActiveStatus(id, status);
        }
    }
}